/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PedidoDialog.java
 *
 * Created on Nov 9, 2010, 9:44:54 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControlePedido;
import br.com.tbp.controle.ControleProduto;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.gui.table.ItensDePedidoTableModel;
import br.com.tbp.gui.table.SituacoesExternasTableModel;
import br.com.tbp.gui.table.JTableUtils;
import br.com.tbp.model.domain.Consultor;
import br.com.tbp.model.domain.Empresa;
import br.com.tbp.model.domain.SituacaoExterna;
import br.com.tbp.model.domain.ItemDePedido;
import br.com.tbp.model.domain.Pedido;
import br.com.tbp.model.domain.Produto;
import br.com.tbp.model.domain.StatusPedido;
import br.com.tbp.model.domain.constant.CondicaoDaLinha;
import br.com.tbp.utils.Atributo;
import br.com.tbp.utils.ImageUtils;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Diego Freire
 */
public class Pedidov1Dialog extends javax.swing.JDialog {

    private ItensDePedidoTableModel model_itens_pedido;
    private SituacoesExternasTableModel model_itens_historico;
    private Pedido pedido;
    private boolean novoPedido;
    private boolean add_info = true;
    private Produto produto_add;
    //private ControleProduto controle_produto;
    private MenuPrincipal menu;
    private JButton buscaConsultorButton, buscaEmpresaButton;

    public Pedidov1Dialog(java.awt.Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        initComponents();
        if (parent instanceof MenuPrincipal) {
            this.menu = (MenuPrincipal) parent;
        }
        //this.controle_produto = new ControleProduto();
        setLocationRelativeTo(null);
        carregaComboProdutos();
        carregaComboSituacoes();
        consultorTextField.add(buscaConsultorButton(), "After");
        empresaTextField.add(buscaEmpresaButton(), "After");
        dataEntradaTextField.setDate(new Date());
        Atributo.reagirEnter(inserirButton);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        contentPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        msgLabel = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        dataEmissaoTextField = new org.jdesktop.swingx.JXDatePicker();
        jPanel2 = new javax.swing.JPanel();
        jPanel43 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        infoLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        consultorTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        empresaTextField = new javax.swing.JTextField();
        infoPanel = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        qtdeTextField = new javax.swing.JTextField();
        inserirButton = new javax.swing.JButton();
        produtosLabel = new javax.swing.JLabel();
        itensLabel = new javax.swing.JLabel();
        jPanel19 = new javax.swing.JPanel();
        produtoButton = new javax.swing.JButton();
        produtoComboBox = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        condicaoComboBox = new javax.swing.JComboBox();
        portInCheckBox = new javax.swing.JCheckBox();
        jLabel8 = new javax.swing.JLabel();
        qtdePortInTextField = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        scrollpane = new javax.swing.JScrollPane();
        itensDePedidoTable = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jPanel15 = new javax.swing.JPanel();
        atualizarButon = new javax.swing.JButton();
        removerButton = new javax.swing.JButton();
        limparButton = new javax.swing.JButton();
        situacaoPanel = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        situacaoCombo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        obsTextArea = new javax.swing.JTextArea();
        inserirSituacaoButton = new javax.swing.JButton();
        atualizarHistoricoButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        dataEntradaTextField = new org.jdesktop.swingx.JXDatePicker();
        cancelarHistoricoButton = new javax.swing.JButton();
        jPanel16 = new javax.swing.JPanel();
        jPanel17 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel18 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        historicoTable = new javax.swing.JTable();
        jPanel10 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        emitirButton = new javax.swing.JButton();
        salvarButton = new javax.swing.JButton();
        fecharButton = new javax.swing.JButton();
        jPanel13 = new javax.swing.JPanel();
        novoPedidoButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        contentPanel.setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        msgLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/venda32.png"))); // NOI18N
        msgLabel.setText("mensagem");
        jPanel4.add(msgLabel);

        jPanel1.add(jPanel4, java.awt.BorderLayout.LINE_START);

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 1, 1, 1));

        jLabel4.setText("Data da Emissão:");
        jPanel5.add(jLabel4);
        jPanel5.add(dataEmissaoTextField);

        jPanel1.add(jPanel5, java.awt.BorderLayout.LINE_END);

        contentPanel.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel43.setLayout(new java.awt.BorderLayout());

        jPanel14.setPreferredSize(new java.awt.Dimension(910, 137));

        infoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/1286892575_addd.png"))); // NOI18N
        infoLabel.setText("Informações");
        infoLabel.setToolTipText("Ver mais informações do cliente");
        infoLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        infoLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                infoLabelMouseClicked(evt);
            }
        });

        jLabel2.setText("Consultor");

        consultorTextField.setFocusable(false);
        consultorTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                consultorTextFieldMouseClicked(evt);
            }
        });

        jLabel3.setText("Empresa");

        empresaTextField.setFocusable(false);
        empresaTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                empresaTextFieldMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(consultorTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
                    .addComponent(infoLabel)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(empresaTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(consultorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(empresaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(infoLabel)
                .addContainerGap())
        );

        jPanel43.add(jPanel14, java.awt.BorderLayout.PAGE_START);

        infoPanel.setLayout(new java.awt.BorderLayout());
        jPanel43.add(infoPanel, java.awt.BorderLayout.PAGE_END);

        jPanel2.add(jPanel43, java.awt.BorderLayout.PAGE_START);

        jPanel6.setLayout(new java.awt.BorderLayout());

        jPanel7.setBackground(new java.awt.Color(195, 223, 251));
        jPanel7.setPreferredSize(new java.awt.Dimension(924, 98));

        jLabel13.setText("Produto");

        jLabel16.setText("Quantidade");

        qtdeTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        qtdeTextField.setText("0");
        qtdeTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                qtdeTextFieldFocusGained(evt);
            }
        });

        inserirButton.setBackground(new java.awt.Color(255, 255, 255));
        inserirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/down.png"))); // NOI18N
        inserirButton.setText("INSERIR");
        inserirButton.setToolTipText("Lança produto no pedido");
        inserirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inserirButtonActionPerformed(evt);
            }
        });

        produtosLabel.setText("Qtde de produtos: 0");

        itensLabel.setText("Qtde de itens: 0");

        jPanel19.setLayout(new java.awt.BorderLayout());

        produtoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/search16.png"))); // NOI18N
        produtoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                produtoButtonActionPerformed(evt);
            }
        });
        jPanel19.add(produtoButton, java.awt.BorderLayout.LINE_START);

        produtoComboBox.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                produtoComboBoxFocusLost(evt);
            }
        });
        jPanel19.add(produtoComboBox, java.awt.BorderLayout.CENTER);

        jLabel7.setText("Condição");

        condicaoComboBox.setModel(new DefaultComboBoxModel(CondicaoDaLinha.values()));

        portInCheckBox.setText("Port In");
        portInCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                portInCheckBoxActionPerformed(evt);
            }
        });

        jLabel8.setText("Quantidade Port In");
        jLabel8.setEnabled(false);

        qtdePortInTextField.setEditable(false);
        qtdePortInTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        qtdePortInTextField.setText("0");
        qtdePortInTextField.setFocusable(false);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(jPanel19, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(produtosLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(itensLabel)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(condicaoComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel16)
                    .addComponent(qtdeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(portInCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(inserirButton, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(qtdePortInTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(12, 12, 12))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(qtdePortInTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(portInCheckBox)))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(condicaoComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel19, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(qtdeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(produtosLabel)
                    .addComponent(itensLabel)
                    .addComponent(inserirButton))
                .addContainerGap())
        );

        jPanel6.add(jPanel7, java.awt.BorderLayout.PAGE_START);

        jPanel8.setLayout(new java.awt.BorderLayout());

        jPanel9.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 3, 0, 1));
        jPanel9.setLayout(new java.awt.BorderLayout());

        scrollpane.setPreferredSize(new java.awt.Dimension(453, 250));

        itensDePedidoTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        itensDePedidoTable.setSelectionBackground(new java.awt.Color(235, 169, 169));
        itensDePedidoTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        itensDePedidoTable.setShowHorizontalLines(false);
        itensDePedidoTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                itensDePedidoTableMouseClicked(evt);
            }
        });
        scrollpane.setViewportView(itensDePedidoTable);

        jPanel9.add(scrollpane, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.BorderLayout());

        atualizarButon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/refresh.png"))); // NOI18N
        atualizarButon.setText("Alterar Quantidade");
        atualizarButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atualizarButonActionPerformed(evt);
            }
        });
        jPanel15.add(atualizarButon);

        removerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/delete.png"))); // NOI18N
        removerButton.setText("Remover Item");
        removerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerButtonActionPerformed(evt);
            }
        });
        jPanel15.add(removerButton);

        limparButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/blank.png"))); // NOI18N
        limparButton.setText("Limpar Itens");
        limparButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparButtonActionPerformed(evt);
            }
        });
        jPanel15.add(limparButton);

        jPanel3.add(jPanel15, java.awt.BorderLayout.LINE_START);

        jPanel9.add(jPanel3, java.awt.BorderLayout.PAGE_START);

        jPanel8.add(jPanel9, java.awt.BorderLayout.CENTER);

        jPanel6.add(jPanel8, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel6, java.awt.BorderLayout.CENTER);

        contentPanel.add(jPanel2, java.awt.BorderLayout.CENTER);

        tabbedPane.addTab("Info. Básicas", contentPanel);

        situacaoPanel.setLayout(new java.awt.BorderLayout());

        jPanel11.setPreferredSize(new java.awt.Dimension(910, 212));

        jLabel1.setText("Escolha a situação atual");

        situacaoCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));

        jLabel5.setText("Observações");

        obsTextArea.setColumns(20);
        obsTextArea.setLineWrap(true);
        obsTextArea.setRows(5);
        obsTextArea.setWrapStyleWord(true);
        jScrollPane1.setViewportView(obsTextArea);

        inserirSituacaoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/down.png"))); // NOI18N
        inserirSituacaoButton.setText("Inserir Situação");
        inserirSituacaoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inserirSituacaoButtonActionPerformed(evt);
            }
        });

        atualizarHistoricoButton.setText("Atualizar");
        atualizarHistoricoButton.setEnabled(false);

        jLabel6.setText("Data de entrada");

        cancelarHistoricoButton.setText("Cancelar");
        cancelarHistoricoButton.setEnabled(false);
        cancelarHistoricoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarHistoricoButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel11Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel11Layout.createSequentialGroup()
                            .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel1)
                                .addComponent(situacaoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 727, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(dataEntradaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6))
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel11Layout.createSequentialGroup()
                            .addComponent(cancelarHistoricoButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(atualizarHistoricoButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(inserirSituacaoButton)
                            .addContainerGap()))))
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(situacaoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dataEntradaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inserirSituacaoButton)
                    .addComponent(atualizarHistoricoButton)
                    .addComponent(cancelarHistoricoButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        situacaoPanel.add(jPanel11, java.awt.BorderLayout.PAGE_START);

        jPanel16.setLayout(new java.awt.BorderLayout());

        jPanel17.setLayout(new java.awt.BorderLayout());
        jPanel17.add(jSeparator1, java.awt.BorderLayout.PAGE_START);

        jPanel16.add(jPanel17, java.awt.BorderLayout.PAGE_START);

        jPanel18.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 3, 1, 3));
        jPanel18.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setPreferredSize(new java.awt.Dimension(453, 300));

        historicoTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        historicoTable.setSelectionBackground(new java.awt.Color(235, 169, 169));
        historicoTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        historicoTable.setShowHorizontalLines(false);
        historicoTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                historicoTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(historicoTable);

        jPanel18.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel16.add(jPanel18, java.awt.BorderLayout.CENTER);

        situacaoPanel.add(jPanel16, java.awt.BorderLayout.CENTER);

        tabbedPane.addTab("Histórico de Situações", situacaoPanel);

        getContentPane().add(tabbedPane, java.awt.BorderLayout.PAGE_START);

        jPanel10.setBackground(new java.awt.Color(255, 255, 255));
        jPanel10.setLayout(new java.awt.BorderLayout());

        jPanel12.setBackground(new java.awt.Color(255, 255, 255));

        emitirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/script.png"))); // NOI18N
        emitirButton.setText("Emitir Pedido");
        emitirButton.setToolTipText("Emitir o pedido...");
        emitirButton.setPreferredSize(new java.awt.Dimension(140, 28));
        emitirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emitirButtonActionPerformed(evt);
            }
        });
        jPanel12.add(emitirButton);

        salvarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/1282921481_001_06.gif"))); // NOI18N
        salvarButton.setText("Salvar");
        salvarButton.setEnabled(false);
        salvarButton.setPreferredSize(new java.awt.Dimension(130, 28));
        salvarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarButtonActionPerformed(evt);
            }
        });
        jPanel12.add(salvarButton);

        fecharButton.setText("Fechar (ESC)");
        fecharButton.setPreferredSize(new java.awt.Dimension(115, 28));
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });
        jPanel12.add(fecharButton);

        jPanel10.add(jPanel12, java.awt.BorderLayout.LINE_END);

        jPanel13.setBackground(new java.awt.Color(255, 255, 255));

        novoPedidoButton.setMnemonic('n');
        novoPedidoButton.setText("Novo Pedido");
        novoPedidoButton.setPreferredSize(new java.awt.Dimension(107, 28));
        novoPedidoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoPedidoButtonActionPerformed(evt);
            }
        });
        jPanel13.add(novoPedidoButton);

        jPanel10.add(jPanel13, java.awt.BorderLayout.LINE_START);

        getContentPane().add(jPanel10, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void infoLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_infoLabelMouseClicked
        if (infoLabel.isEnabled()) {
            if (infoPanel.getComponentCount() > 0) {
                infoPanel.removeAll();
                infoPanel.repaint();
            }
            if (add_info) {
                infoPanel.add(new DetalhesDaEmpresaPanel(this.objetoPedido().getEmpresa()), "First");
                infoPanel.repaint();
                add_info = false;
            } else {
                add_info = true;
            }

            this.pack();
            this.setLocationRelativeTo(null);
        }
    }//GEN-LAST:event_infoLabelMouseClicked

    private void inserirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inserirButtonActionPerformed
        this.validaItem();
}//GEN-LAST:event_inserirButtonActionPerformed

    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_fecharButtonActionPerformed

    private void qtdeTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_qtdeTextFieldFocusGained
        qtdeTextField.selectAll();
    }//GEN-LAST:event_qtdeTextFieldFocusGained

    private void atualizarButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atualizarButonActionPerformed
        alterarQuantidadeDoItem(itensDePedidoTable.getSelectedRow());
}//GEN-LAST:event_atualizarButonActionPerformed

    private void removerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerButtonActionPerformed
        int row = itensDePedidoTable.getSelectedRow();
        if (row >= 0) {
            ItemDePedido item = model_itens_pedido.getItem(row);
            this.objetoPedido().retiraItem(item);
            refreshItensDePedido();
        }
}//GEN-LAST:event_removerButtonActionPerformed

    private void limparButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparButtonActionPerformed
        this.objetoPedido().limparItens();
        refreshItensDePedido();
}//GEN-LAST:event_limparButtonActionPerformed

    private void salvarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarButtonActionPerformed
        try {
            Pedido p = this.objetoPedido();
            p.setData(dataEmissaoTextField.getDate());
            ControlePedido controle_pedido = new ControlePedido();
            if (isNovoPedido()) {
                controle_pedido.registrar(p);
                msgLabel.setText("O pedido nº " + p.getId() + " foi salvo com sucesso");
                bloqueiaFinalizacao(true);
                //emitirButton.setEnabled(true);
                salvarButton.setEnabled(false);
            } else {
                controle_pedido.atualizar(p);
                msgLabel.setText("O pedido nº " + p.getId() + " foi atualizado com sucesso");
            }
            tabbedPane.setSelectedIndex(0);
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }//GEN-LAST:event_salvarButtonActionPerformed

    private void novoPedidoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novoPedidoButtonActionPerformed
        setNovoPedido(true);
        setPedido(new Pedido());
    }//GEN-LAST:event_novoPedidoButtonActionPerformed

    private void emitirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emitirButtonActionPerformed
        //GerenciadorDeRelatorio.emitirPedido(this.objetoPedido().getId());
        //GerenciadorDeRelatorio.emitirPedido((long) 9);
    }//GEN-LAST:event_emitirButtonActionPerformed

    private void itensDePedidoTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_itensDePedidoTableMouseClicked
        if (evt.getClickCount() == 2) {
            int row = itensDePedidoTable.getSelectedRow();
            if (row >= 0) {
                ItemDePedido item = model_itens_pedido.getItem(row);
            }
        }
    }//GEN-LAST:event_itensDePedidoTableMouseClicked

    private void produtoComboBoxFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_produtoComboBoxFocusLost
        this.produto_add = (Produto) produtoComboBox.getSelectedItem();
    }//GEN-LAST:event_produtoComboBoxFocusLost

    private void empresaTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_empresaTextFieldMouseClicked
        if (buscaEmpresaButton.isEnabled()) {
            buscaEmpresa();
        }
    }//GEN-LAST:event_empresaTextFieldMouseClicked

    private void consultorTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_consultorTextFieldMouseClicked
        if (buscaConsultorButton.isEnabled()) {
            buscaConsultor();
        }
    }//GEN-LAST:event_consultorTextFieldMouseClicked

    private void inserirSituacaoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inserirSituacaoButtonActionPerformed
        StatusPedido situacao = (StatusPedido) situacaoCombo.getSelectedItem();
        if (situacao == null) {
            JOptionPane.showMessageDialog(null, "A situação não foi escolhida", "Atenção", JOptionPane.WARNING_MESSAGE);
            situacaoCombo.requestFocus();
            return;
        }
        Pedido p = this.objetoPedido();
        Date data = dataEntradaTextField.getDate();
        String obs = obsTextArea.getText();
        SituacaoExterna item = new SituacaoExterna(situacao, data, obs, p, true);
        p.addSituacaoExternaNova(item);
        refreshItensDeHistorico();
        mostraHistorico(null);
    }//GEN-LAST:event_inserirSituacaoButtonActionPerformed

    private void historicoTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_historicoTableMouseClicked
        if (evt.getClickCount() == 2) {
            int row = historicoTable.getSelectedRow();
            if (row >= 0) {
                SituacaoExterna item = this.objetoPedido().getSituacaoExterna(row);
                if (item != null) {
                    mostraHistorico(item);
                    liberaAtualizacaoDeHistorico(true);
                }
            }
        }
    }//GEN-LAST:event_historicoTableMouseClicked

    private void cancelarHistoricoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarHistoricoButtonActionPerformed
        liberaAtualizacaoDeHistorico(false);
    }//GEN-LAST:event_cancelarHistoricoButtonActionPerformed

    private void produtoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_produtoButtonActionPerformed
        if (produtoButton.isEnabled()) {
            ProdutosDialog dialog = new ProdutosDialog(this.menu, true);
            dialog.setSelect(true);
            dialog.setVisible(true);
            this.mostraProduto(dialog.getProduto());
            qtdeTextField.requestFocus();
        }
    }//GEN-LAST:event_produtoButtonActionPerformed

    private void portInCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_portInCheckBoxActionPerformed
        boolean state = portInCheckBox.isSelected();
        qtdePortInTextField.setEditable(state);
        qtdePortInTextField.setFocusable(state);
        jLabel8.setEnabled(state);
        qtdePortInTextField.setText("0");
        qtdePortInTextField.requestFocus(state);
    }//GEN-LAST:event_portInCheckBoxActionPerformed
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton atualizarButon;
    private javax.swing.JButton atualizarHistoricoButton;
    private javax.swing.JButton cancelarHistoricoButton;
    private javax.swing.JComboBox condicaoComboBox;
    private javax.swing.JTextField consultorTextField;
    private javax.swing.JPanel contentPanel;
    private org.jdesktop.swingx.JXDatePicker dataEmissaoTextField;
    private org.jdesktop.swingx.JXDatePicker dataEntradaTextField;
    private javax.swing.JButton emitirButton;
    private javax.swing.JTextField empresaTextField;
    private javax.swing.JButton fecharButton;
    private javax.swing.JTable historicoTable;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JButton inserirButton;
    private javax.swing.JButton inserirSituacaoButton;
    private javax.swing.JTable itensDePedidoTable;
    private javax.swing.JLabel itensLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel43;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton limparButton;
    private javax.swing.JLabel msgLabel;
    private javax.swing.JButton novoPedidoButton;
    private javax.swing.JTextArea obsTextArea;
    private javax.swing.JCheckBox portInCheckBox;
    private javax.swing.JButton produtoButton;
    private javax.swing.JComboBox produtoComboBox;
    private javax.swing.JLabel produtosLabel;
    private javax.swing.JTextField qtdePortInTextField;
    private javax.swing.JTextField qtdeTextField;
    private javax.swing.JButton removerButton;
    private javax.swing.JButton salvarButton;
    private javax.swing.JScrollPane scrollpane;
    private javax.swing.JComboBox situacaoCombo;
    private javax.swing.JPanel situacaoPanel;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    public void setPedido(Pedido p) {
        this.pedido = p;
        dataEmissaoTextField.setDate(new Date(p.getData().getTime()));
        consultorTextField.setText(p.getConsultor() != null ? p.getConsultor().getNome() : null);
        empresaTextField.setText(p.getEmpresa() != null ? p.getEmpresa().getNomeFantasia() : null);

        model_itens_pedido = new ItensDePedidoTableModel(p);
        itensDePedidoTable.setModel(model_itens_pedido);

        model_itens_historico = new SituacoesExternasTableModel(p);
        historicoTable.setModel(model_itens_historico);

        refreshItensDePedido();
        refreshItensDeHistorico();
    }

    public boolean isNovoPedido() {
        return novoPedido;
    }

    public void setNovoPedido(boolean novoPedido) {
        this.novoPedido = novoPedido;
        if (isNovoPedido()) {
            msgLabel.setText("Forneça os dados para o novo pedido");
            emitirButton.setEnabled(false);
            buscaConsultorButton.setEnabled(true);
            bloqueiaFinalizacao(true);
        } else {
            msgLabel.setText("Visualização dos dados do pedido nº " + this.objetoPedido().getId());
            emitirButton.setEnabled(true);
            salvarButton.setEnabled(true);
            buscaConsultorButton.setEnabled(false);
            buscaEmpresaButton.setEnabled(false);
        }

        mostraProduto(null);
    }

    private void validaItem() {
        if (produto_add != null) {
            if (!produto_add.isAtivo()) {
                JOptionPane.showMessageDialog(null, "O produto " + produto_add.getNome() + " está desativado.",
                        "Atenção", JOptionPane.WARNING_MESSAGE);
                return;
            }
            ItemDePedido item =
                    new ItemDePedido(produto_add, 0);
            /*
            boolean contem = this.objetoPedido().contemItem(item);
            if (contem) {
            JOptionPane.showMessageDialog(null, "O item " + item.getProduto().getNome()
            + " já consta na lista do pedido\ne não pode ser inserido novamente.",
            "Aviso", JOptionPane.WARNING_MESSAGE);
            mostraProduto(null);
            return;
            }
             */
            try {
                int qtde = Integer.parseInt(qtdeTextField.getText());
                if (qtde < 1) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException nfe) {
                qtdeTextField.setText("0");
                qtdeTextField.requestFocus();
                qtdeTextField.selectAll();
                return;
            }

            if (portInCheckBox.isSelected()) {
                try {
                    int qtde = Integer.parseInt(qtdePortInTextField.getText());
                    if (qtde < 1) {
                        throw new NumberFormatException();
                    }
                } catch (NumberFormatException nfe) {
                    qtdePortInTextField.setText("0");
                    qtdePortInTextField.requestFocus();
                    qtdePortInTextField.selectAll();
                    return;
                }
            }
            item.setQuantidade(Integer.parseInt(qtdeTextField.getText()));
            item.setCondicao((CondicaoDaLinha) condicaoComboBox.getSelectedItem());
            if (portInCheckBox.isSelected()) {
                item.setPortIn(true);
                item.setQtePortIn(Integer.parseInt(qtdePortInTextField.getText()));
            }
            this.addItem(item);
        } else {
            produtoComboBox.requestFocus();
        }
    }

    private Pedido objetoPedido() {
        return this.pedido;
    }

    private void refreshItensDePedido() {
        model_itens_pedido.fireTableDataChanged();
        JTableUtils.fitAllColumns(itensDePedidoTable);
        Pedido p = this.objetoPedido();
        produtosLabel.setText("Qtde de produtos: " + p.qtde_de_produtos());
        itensLabel.setText("Qtde de itens: " + p.qtde_de_itens());

        if (isNovoPedido()) {
            qtdeTextField.setText("0");
            salvarButton.setEnabled(p.qtde_de_produtos() > 0);
        }
    }

    private void refreshItensDeHistorico() {
        model_itens_historico.fireTableDataChanged();
        JTableUtils.fitAllColumnsWithoutRenderer(historicoTable);
    }

    public Pedido getPedido() {
        pedido.setData(dataEmissaoTextField.getDate());
        return pedido;
    }

    private void setEmpresa(Empresa e) {
        this.objetoPedido().setEmpresa(e);
        empresaTextField.setText(e != null ? e.getNomeFantasia() : null);
        bloqueiaFinalizacao(e == null);
    }

    private void setConsultor(Consultor c) {
        this.objetoPedido().setConsultor(c);
        consultorTextField.setText(c != null ? c.getNome() : null);
    }

    private void mostraProduto(Produto p) {
        this.produto_add = p;
        if (p != null) {
            produtoComboBox.setSelectedItem(p);
            qtdeTextField.setText("0");
        } else {
            produtoComboBox.setSelectedItem(null);
            qtdeTextField.setText("0");
            produtoComboBox.requestFocus();
            condicaoComboBox.setSelectedIndex(0);
            portInCheckBox.setSelected(false);
            jLabel8.setEnabled(false);
            qtdePortInTextField.setEditable(false);
            qtdePortInTextField.setFocusable(false);
            qtdePortInTextField.setText("0");
        }
    }

    private void addItem(ItemDePedido item) {
        Pedido p = this.objetoPedido();
        p.addItem(item);
        refreshItensDePedido();
        this.mostraProduto(null);
    }

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fecharButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }

    private void alterarQuantidadeDoItem(int row) {
        if (row >= 0) {
            ItemDePedido item = model_itens_pedido.getItem(row);
            String str = (String) JOptionPane.showInputDialog(null,
                    "Nova Quantidade: ", "Alterar Quantidade '" + item.getProduto().getNome() + "'", JOptionPane.WARNING_MESSAGE,
                    null, null, item.getQtde());
            try {
                int qtde = Integer.parseInt(str);
                this.objetoPedido().alterarQuantidadeDoItem(item, qtde);
                refreshItensDePedido();
            } catch (NumberFormatException nfe) {
                return;
            }
        }
    }

    private void bloqueiaFinalizacao(boolean b) {
        b = !b;
        produtoComboBox.setEnabled(b);
        produtoComboBox.setFocusable(b);
        infoLabel.setEnabled(b);
        produtoButton.setEnabled(b);
        qtdeTextField.setEditable(b);
        condicaoComboBox.setEnabled(b);
        portInCheckBox.setEnabled(b);
        inserirButton.setEnabled(b);
        produtosLabel.setEnabled(b);
        itensLabel.setEnabled(b);
        itensDePedidoTable.setEnabled(b);
        atualizarButon.setEnabled(b);
        removerButton.setEnabled(b);
        limparButton.setEnabled(b);
    }

    public MenuPrincipal getMenu() {
        return menu;
    }

    private void carregaComboProdutos() {
        try {
            ControleProduto controle = new ControleProduto();
            DefaultComboBoxModel combo_model = new DefaultComboBoxModel(controle.pesquisar().toArray());
            produtoComboBox.setModel(combo_model);
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }

    private void carregaComboSituacoes() {
        try {
            //ControleProduto controle = new ControleProduto();
            //DefaultComboBoxModel combo_model = new DefaultComboBoxModel(StatusPedido.createStatus().toArray());
            //situacaoCombo.setModel(combo_model);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    private JButton buscaConsultorButton() {
        consultorTextField.setLayout(new BorderLayout());
        buscaConsultorButton = new JButton(ImageUtils.createIcon("search16.png"));
        buscaConsultorButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        buscaConsultorButton.setToolTipText("Clique para buscar um consultor");

        buscaConsultorButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                buscaConsultor();
            }
        });
        return buscaConsultorButton;
    }

    private JButton buscaEmpresaButton() {
        empresaTextField.setLayout(new BorderLayout());
        buscaEmpresaButton = new JButton(ImageUtils.createIcon("search16.png"));
        buscaEmpresaButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        buscaEmpresaButton.setToolTipText("Clique para buscar uma empresa");

        buscaEmpresaButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                buscaEmpresa();
            }
        });
        return buscaEmpresaButton;
    }

    private void buscaConsultor() {
        ConsultoresDialog dialog = new ConsultoresDialog(menu, true);
        dialog.setSelect(true);
        dialog.setVisible(true);
        setConsultor(dialog.getConsultor());
    }

    private void buscaEmpresa() {
        EmpresasDialog dialog = new EmpresasDialog(menu, true);
        dialog.setSelect(true);
        dialog.setVisible(true);
        setEmpresa(dialog.getEmpresa());
    }

    private void mostraHistorico(SituacaoExterna item) {
        if (item != null) {
            situacaoCombo.setSelectedItem(item.getSituacao());
            dataEntradaTextField.setDate(item.getData());
            obsTextArea.setText(item.getObservacoes());
        } else {
            situacaoCombo.setSelectedIndex(-1);
            dataEntradaTextField.setDate(new Date());
            obsTextArea.setText(null);
        }
        obsTextArea.setCaretPosition(0);
        situacaoCombo.requestFocus();
    }

    private void liberaAtualizacaoDeHistorico(boolean b) {
        atualizarHistoricoButton.setEnabled(b);
        cancelarHistoricoButton.setEnabled(b);
        inserirSituacaoButton.setEnabled(!b);
    }
}
